<?php

class News extends Controller {

    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url().'/admin.php?c=auth&m=index');
        }
        $this->load->model('newsmodel', 'news');
    }

    public function index() {
        $this->load->library('pagination');

        $limit = 2;
        $total = $this->news->n_count();
        $offset = intval($this->input->get('per_page'));

        $config['base_url'] = base_url().'/admin.php?c=news&m=index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '久灵资讯';
        $data['news'] = $this->news->n_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();
        $this->load->view('news/index', $data);
    }

    public function create() {
        $this->load->helper('form');
        $data['title'] = '添加新闻';
        $this->load->view('news/create', $data);
    }

    public function add() {
        $this->load->library('form_validation');
		$this->form_validation->set_rules('title', '资讯标题', 'required');
		$this->form_validation->set_rules('content', '资讯内容', 'required');
        if ($this->form_validation->run() == false) {
            $data['title'] = '添加新闻';
            $this->load->view('news/create', $data);
        } else {
            $news['title'] = $this->input->post('title');
            $news['content'] = $this->input->post('content');
            $news['created_at'] = date('Y-m-d H:i:s', time());
            $this->db->insert('news', $news);
            redirect(base_url().'/admin.php?c=news&m=index');
        }
    }

    public function edit() {
        $this->load->helper('form');
        $id = $this->getId();
        if ($news = $this->news->find($id)) {
            $data['title'] = '编辑【' . $news->title . '】';
            $data['news'] = $news;
            $this->load->view('news/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));
        if ($n = $this->news->find($id)) {
            $this->load->library('form_validation');
			$this->form_validation->set_rules('title', '资讯标题', 'required');
			$this->form_validation->set_rules('content', '资讯内容', 'required');
            if ($this->form_validation->run() == false) {
                $data['news'] = $n;
                $data['title'] = '编辑' . $n->title;
                $this->load->view('news/edit', $data);
            } else {
                $news['title'] = $this->input->post('title');
                $news['content'] = $this->input->post('content');
                if ($this->db->update('news', $news, array('id' => $id))) {
                    $this->session->set_flashdata('notice', '修改新增成功！');
                    redirect(base_url().'/admin.php?c=news&m=index');
                } else {
                    $this->session->set_flashdata('error', '数据库错误！');
                    redirect(base_url().'/admin.php?c=news&m=index');
                }
            }
        } else {
            show_404();
        }
    }

    public function delete() {
        $id = $this->getId();
        if ($this->news->find($id)) {
            if ($this->db->delete('news', array('id' => $id))) {
                $this->session->set_flashdata('notice', '删除成功！');
                redirect(base_url().'/admin.php?c=news&m=index');
            }
        } else {
            show_404();
        }
    }

    private function getId() {
        return intval($this->input->get('id'));
    }

}